package cn.newtouch.product.service.impl;

import cn.newtouch.basic.exception.BusinessException;
import cn.newtouch.basic.service.IBaiduAiAuditService;
import cn.newtouch.basic.service.impl.BaseServiceImpl;
import cn.newtouch.product.domain.Product;
import cn.newtouch.product.domain.ProductDetail;
import cn.newtouch.product.mapper.ProductDetailMapper;
import cn.newtouch.product.mapper.ProductMapper;
import cn.newtouch.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;


    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public void save(Product product) {

        Boolean textBl = baiduAiAuditService.textAudit(product.getName());

        Boolean imgBl = baiduAiAuditService.imageAudit(product.getResources());

        if(textBl && imgBl){//3.审核通过
            //保存product表 返回自增主键
            productMapper.save(product);

            //保存productDetail表
            ProductDetail productDetail = product.getProductDetail();
            productDetail.setProduct_id(product.getId());
            productDetailMapper.save(productDetail);

        }else{//4.审核不通过

            throw  new BusinessException("审核不通过，已驳回！！");
        }
    }

    @Override
    public void update(Product product) {
        //审核
        Boolean textBl = baiduAiAuditService.textAudit(product.getName());

        Boolean imgBl = baiduAiAuditService.imageAudit(product.getResources());

        if(textBl && imgBl){//3.审核通过
            //修改product
            productMapper.update(product);
            //修改productDetail详情表
            productDetailMapper.update(product.getProductDetail());

        }else{//4.审核不通过

            throw  new BusinessException("审核不通过，已驳回！！");
        }
    }

    @Override
    public void remove(Long id) {
        //先删除productDetail表
        productDetailMapper.removeByProductId(id);
        //再删除product表
        productMapper.remove(id);
    }



    /**
     * 上架
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        //再改状态 上架
        Date date = new Date();
        productMapper.onSale(ids,date);
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        //改状态 下架
        Date date = new Date();
        productMapper.offSale(ids,date);

    }
}
